package com.hyf.serviceedu.controller;


import com.hyf.commonutils.fruit;
import com.hyf.service.config.exception.GuliException;
import com.hyf.serviceedu.Microservice.VodClient;
import com.hyf.serviceedu.entity.EduChapter;
import com.hyf.serviceedu.entity.EduVideo;
import com.hyf.serviceedu.entity.condition.VideoInfoForm;
import com.hyf.serviceedu.service.EduChapterService;
import com.hyf.serviceedu.service.EduVideoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 课程视频 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2021-04-21
 */
@RestController
@RequestMapping("/serviceedu/edu-video")
//@CrossOrigin// 跨域
@Api(description="课时管理")
public class EduVideoController {

    @Autowired
    private EduVideoService eduVideoService;
    @Autowired
    private EduChapterService eduChapterService;

    // 注入 VodClient nacos微服务的一个调用
    @Autowired
    private VodClient vodClient;

    // 添加小结
    @ApiOperation(value = "添加小结")
    @PostMapping("addVideo")
    public fruit addVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.save(eduVideo);
        return fruit.ok();
    }


    // 删除小结
    //  后面这个方法需要完善，删除小结的时候，同时把视频也删除掉  已经完善
    @ApiOperation(value = "根据ID删除小结")
    @DeleteMapping("{id}")
    public fruit deleteVideo(@PathVariable String id){
        
        // 根据小结id 拿到 视频id，然后调用方法实现视频删除
        EduVideo eduVideo = eduVideoService.getById(id);// 先通过id查询出所有的内容
        String videoSourceId = eduVideo.getVideoSourceId();// 拿到了视频的id


        // 在这里直接写到controller中不写 service中了
        // 判断小结里面是否有视频ID
        if(!StringUtils.isEmpty(videoSourceId)){// 如果说视频id 不等于空就调用删除视频的方法，等于空的话则不调用
            // 直接调用方法
            // 通过视频id删除视频的方法,远程调用实现删除
            fruit fit = vodClient.removeAlyVideo(videoSourceId);
// 当调用过程中 返回的状态吗 为 20001，则删除视频失败
            if(fit.getCode()==20001){
                throw new GuliException(20001,"删除视频失败，熔断器......");
            }


        }
        // 删除小结
        eduVideoService.removeById(id);
        return fruit.ok();
    }





    // 修改小结 TODO
    // 先根据id 进行查询

    @ApiOperation(value = "根据ID查询小结")
    @GetMapping("getVideoinfo/{id}")
    public fruit getVideoinfo(
            @ApiParam(name = "id", value = "章节ID", required = true)
            @PathVariable String id){
        EduVideo Video = eduVideoService.getById(id);
        return fruit.ok().data("Video",Video);

    }


    // 修改章节
    @ApiOperation(value = "修改小节")
    @PostMapping("updateVideo")
    public fruit updateVideo(@RequestBody EduVideo eduVideo){
        eduVideoService.updateById(eduVideo);
        return fruit.ok();

    }












// 分解id 查询章节完整版
    @ApiOperation(value = "根据ID查询课时")
    @GetMapping("video-info/{id}")
    public fruit getVideInfoById(
@ApiParam(name = "id", value = "课时ID", required = true)
        @PathVariable String id){
        VideoInfoForm videoInfoForm = eduVideoService.getVideoInfoFormById(id);
        return fruit.ok().data("item", videoInfoForm);
    }






    // 完整版修改
    @ApiOperation(value = "根据ID修改章节")
    @PutMapping("{id}")
    public fruit updateById(
@ApiParam(name = "id", value = "章节ID", required = true)
        @PathVariable String id,
@ApiParam(name = "chapter", value = "章节对象", required = true)
        @RequestBody EduChapter chapter){
        chapter.setId(id);
        eduChapterService.updateById(chapter);
        return fruit.ok();
    }

}

